<?php

function sortMA ($a, $b) {
  if ($b->ma == $a->ma) {
    return 0;
  } else {
    return $b->ma < $a->ma ? -1 : 1;
  }
}

function sortST ($a, $b) {
  if ($b->st == $a->st) {
    return 0;
  } else {
    return $b->st < $a->st ? -1 : 1;
  }
}

function sortAG ($a, $b) {
  if ($b->ag == $a->ag) {
    return 0;
  } else {
    return $b->ag < $a->ag ? -1 : 1;
  }
}

function sortAV ($a, $b) {
  if ($b->av == $a->av) {
    return 0;
  } else {
    return $b->av < $a->av ? -1 : 1;
  }
}

function sortCost ($a, $b) {
  if ($b->cost == $a->cost) {
    return 0;
  } else {
    return $b->cost < $a->cost ? -1 : 1;
  }
}

function sortBest11 ($a, $b) {
  if ($b->getBest11Cost() == $a->getBest11Cost()) {
    return 0;
  } else {
    return $b->getBest11Cost() < $a->getBest11Cost() ? -1 : 1;
  }
}

function costSort($a, $b) {
  if ($b == $a) {
    return 0;
  } else {
    return $b < $a ? -1 : 1;
  }
}

class TeamAverage {

  var $ma = 0;
  var $st = 0;
  var $ag = 0;
  var $av = 0;
  var $cost = 0;
  var $costs = array();
  var $team;

  function TeamAverage(&$team) {
    $this->team = $team;
    $this->getTeamAverages();
  }

  function getRound($value, $div) {
    return round($value / ($div == 0 ? 1 : $div), 2);
  }

  function addPlayer($player) {
    $this->ma += $player->ma;
    $this->st += $player->st;
    $this->ag += $player->ag;
    $this->av += getProb(1 + $player->av);
    $this->cost += $player->cost;
    $this->addPlayerCost($player->cost);
  }

  function getBest11Cost() {
    $total = 0;
    foreach($this->costs as $cost) {
      $total += $cost;
    }

    return $this->getRound($total, sizeof($this->costs));
  }

  function addPlayerCost($cost) {
    if (sizeof($this->costs) < 11) {
      array_push($this->costs, $cost);
    } else {
      $lowest = array_pop($this->costs);
      array_push($this->costs, ($cost > $lowest ? $cost : $lowest));
    }
    uasort ($this->costs, "costSort");
  }

  function getTeamAverages() {

    foreach($this->team->players as $player) {
      $this->addPlayer($player);
    }
    $count = sizeof($this->team->players);

    $this->ma = $this->getRound($this->ma, $count);
    $this->st = $this->getRound($this->st, $count);
    $this->ag = $this->getRound($this->ag, $count);
    $this->av = $this->av == 0 ? 0 : $this->getArmourRanking($this->av, $count);
    $this->cost = $this->getRound($this->cost, $count);
  }

  function getAdjustedFraction($averageProb, $probRequired, $gap) {
      return $this->getRound($averageProb - $probRequired, $gap);
  }

  function getArmourRanking($totalAv, $count) {
    $averageProb = $this->getRound($totalAv, $count);
    $baseNum = 0;
    $remainingProb = 0;
    $prev = 0;
    if ($averageProb <= 3) {
      return  11 - $this->getAdjustedFraction($averageProb, 1, 2);
    } else if ($averageProb <= 6) {
      return  10 - $this->getAdjustedFraction($averageProb, 3, 3);
    } else if ($averageProb <= 10) {
      return  9 - $this->getAdjustedFraction($averageProb, 6, 4);
    } else if ($averageProb <= 15) {
      return  8 - $this->getAdjustedFraction($averageProb, 10, 5);
    } else if ($averageProb <= 21) {
      return  7 - $this->getAdjustedFraction($averageProb, 15, 6);
    } else {
      return  6 - $this->getAdjustedFraction($averageProb, 21, 5);
    }
  }

  function echoRow($row) {
    echo getEchoTR($row, "center");
    echo "  <td>" . $this->team->getRosterLink()  ."</td>\n";
    echo "  <td>" . getEchoValue($this->team->race->name) . "</td>\n";
    echo "  <td>" . getEchoValue($this->ma) . "</td>\n";
    echo "  <td>" . getEchoValue($this->st) . "</td>\n";
    echo "  <td>" . getEchoValue($this->ag) . "</td>\n";
    echo "  <td>" . getEchoValue($this->av) . "</td>\n";
    echo "  <td>" . getEchoValue($this->cost) . "</td>\n";
    echo "  <td>" . getEchoValue($this->getBest11Cost()) . "</td>\n";
    echo "</tr>\n";
  }


}
?>